/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>Project: javaseapp - Stream6
 * <p>Powered by webrx On 2021-08-23 14:48:38
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class Stream6 {
    public static void main(String[] args) {
        //List<Student> 转 Stream<Student>
        var list = new ArrayList<Student>(List.of(new Student(1, "李四", 30), new Student(2, "王五", 62), new Student(3, "张三", 50), new Student(4, "赵平", 80), new Student(5, "李强", 90), new Student(6, "李四", 80)));
        System.out.println(list);
        var sm = list.stream();

        //Stream<Student> 转 List<Student>
        //var list2 = sm.sorted((a, b) -> b.getId() - a.getId()).collect(Collectors.toList());
        //list2.add(new Student(6, "李四", 55));
        //System.out.println(list2);

        //Stream<Student> 转 List<String> 全是姓名
        //var list3 = sm.map(Student::getName).collect(Collectors.toList());
        //System.out.println(list3);


        //Stream<Student> 转 Set<String> 全是姓名
        //var set = sm.map(Student::getName).collect(Collectors.toSet());
        //System.out.println(set);

        //Stream<Student> 转 Map<String,Student>
        //var m = sm.collect(Collectors.toMap(e -> "stu_" + e.getId(), e -> e));
        //System.out.println(m);

        //Stream<Student> 转 String
        //var str = sm.map(Student::getName).collect(Collectors.joining("-"));
        //var str = sm.map(Student::getName).collect(Collectors.joining());
        //var str = sm.map(e -> String.format("\"%s\"", e.getName())).collect(Collectors.joining(",", "[", "]"));
        //System.out.println(str);

        //long c = sm.collect(Collectors.counting());
        //System.out.println(c);
        //System.out.println(sm.count());

        Stream<Integer> si = Stream.of(1, 2, 3, 4);
        //System.out.println(si.max(Comparator.comparingInt(a -> a)).get());
        //System.out.println(si.min(Comparator.comparingInt(a -> a)).get());
        //求和
        //System.out.println(si.collect(Collectors.summingInt(a -> a)));

        //求平均值
        System.out.println(si.collect(Collectors.averagingInt(a -> a)));

    }
}
